﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Default.master" AutoEventWireup="true" CodeBehind="Search.aspx.cs" Inherits="ecommerceSample.SearchPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <div class="divSearch">
        <asp:Menu ID="Menu1" runat="server"
            Orientation="Horizontal"
            StaticEnableDefaultPopOutImage="False" BackColor="#35302d" ForeColor="#8d8d8d" 
            Font-Names="font-family:Arial, Helvetica, Sans-Serif" Font-Size="Medium"
            DisappearAfter="150" DynamicHorizontalOffset="2" 
            CssClass="search-menu" StaticSubMenuIndent="10px" 
            onmenuitemclick="Menu1_MenuItemClick">
            <Items>
                <asp:MenuItem Text="Busqueda Simple" Value="0"   Selected="True"></asp:MenuItem>            
                <asp:MenuItem Text="Busqueda Por Campos" Value="1" ></asp:MenuItem>
                <asp:MenuItem Text="Indice por Categorías" Value="2" ></asp:MenuItem>
            </Items>
            <StaticHoverStyle BackColor="#35302d" ForeColor="#92cb4b"/>
            <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
            <StaticSelectedStyle BackColor="#35302d" ForeColor="#92cb4b"/>
        </asp:Menu>

        <asp:MultiView ID="MuVSearch" runat="server" ActiveViewIndex="0">
            <asp:View ID="vwSimpleSearch" runat="server">
                <asp:Panel ID="pnlSimpleSearch" runat="server">                
                    <table style="width: 505px">
                        <tr>
                            <td><br />
                                <asp:TextBox ID="txBSimpleSearch" runat="server" Width="336px"></asp:TextBox>
                            </td>            
                            <td><br />
                                <asp:Button ID="btnSimpleSearch" runat="server" Text="Buscar" 
                                 onclick="btnSimpleSearch_Click" />
                            </td>
                            <td><br />
                                <asp:RequiredFieldValidator ID="rfvSimpleSearch" runat="server" 
                                ErrorMessage="Requerido" ControlToValidate="txBSimpleSearch" 
                                ForeColor="Red"></asp:RequiredFieldValidator>
                            </td>
                        </tr>        
                    </table>
                </asp:Panel>

                <asp:Panel ID="pnlSimpleResult" runat="server" Visible="False">
                    <asp:SqlDataSource ID="SqlDataSource3" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:ecommerceSample %>"                         
                        SelectCommand="SELECT Articles.Title, Categories.Name, Categories.CategoryId, Articles.Image, Articles.Text, Articles.Date, aspnet_Users.UserName, aspnet_Users.UserId, Articles.ArticleId FROM Articles INNER JOIN Categories ON Articles.CategoryId = Categories.CategoryId INNER JOIN aspnet_Users ON Articles.UserId = aspnet_Users.UserId WHERE (Articles.Title LIKE N'%' + @title + N'%')">
                        <SelectParameters>
                            <asp:ControlParameter ControlID="txBSimpleSearch" Name="title" 
                                PropertyName="Text" />
                        </SelectParameters>
                    </asp:SqlDataSource>
                    <table style="width: 505px">
                        <tr>
                            <td><br />
                                <asp:Button ID="btnReturnSimpleSearch" runat="server" onclick="Button1_Click" Text="Ver Formulario" />
                            <br /></td>      
                        </tr>        
                    </table>
                    <asp:GridView ID="gvSimpleSearch" runat="server" AllowPaging="True" BorderColor="White"
                        AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" DataKeyNames="ArticleId" 
                        DataSourceID="SqlDataSource3">
                        <Columns>
                            <asp:TemplateField>
                                <ItemTemplate>
                                <asp:Table ID="Table1" runat="server">
                                    <asp:TableRow ID="TableRow1" runat="server">
                                        <asp:TableCell ID="TableCell1" runat="server" >
                                             <a href="/User/Article.aspx?id=<%# Eval("ArticleId") %>"><img alt="<%# Eval("Title") %>" class="small-images"  src="<%# Eval("Image") %>" width="120px"/></a>
                                        </asp:TableCell>
                                        <asp:TableCell ID="TableCell2" runat="server" CssClass="text-content">                                            
                                            <hr />
                                            <div class="article-grey"><a href="/Category.aspx?id=<%# Eval("CategoryId") %>"><%# Eval("Name") %></a><small>: <%# Eval("Date") %></small></div>
                                                <b>
                                                <div class="article-title"><big>
                                                <a href="/User/Article.aspx?id=<%# Eval("ArticleId") %>"><%# MarcarPalabra(Eval("Title").ToString())%></a></big></div>
                                                </b>
                                            <small>
                                                <div class="article-grey">Posteado por: <big>
                                                <a href="/User/User.aspx?id=<%# Eval("UserId") %>"><%# Eval("UserName") %></a></big></div>
                                                <%# LimitWords(Eval("Text").ToString(),50)%>                                            
                                                <a href="/User/Article.aspx?id=<%# Eval("ArticleId") %>"><br />
                                                <div class="read-more"><i>leer más</i></a></div>
                                            </small>
                                        </asp:TableCell>
                                    </asp:TableRow>
                                </asp:Table>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <asp:Label ID="lblSimpleSearchError" runat="server" BorderColor="White" 
                                BorderStyle="None" Font-Bold="True" ForeColor="Red" 
                                Text="No existe resultados"></asp:Label>
                        </EmptyDataTemplate>
                    </asp:GridView>                          
                </asp:Panel>
            </asp:View>
            
            <asp:View ID="vwAdvancedSearch" runat="server">
            <asp:Panel ID="pnlAdvancedSearch" runat="server">
               
            <table>
            <tr>
                <td width="15%">
                    <asp:SqlDataSource ID="SqlDataSource5" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:ecommerceSample %>" 
                        SelectCommand="SELECT [Name] FROM [Categories] ORDER BY [Name]">
                    </asp:SqlDataSource>
                </td>

                <td>
                    <table cellpadding="0" cellspacing="0">
                        <tr>

                            <td align="left">                    
                    <asp:Label ID="lblAdvancedSearch" runat="server" Text="Búsqueda" Font-Bold="True"></asp:Label>
                </td>
                            <td>
                    <asp:Label ID="lblSet" runat="server" Text="Campos" Font-Bold="True"></asp:Label>
                </td>
                        </tr>

                        <tr>
                          
                            <td>                    
                    <asp:TextBox ID="txbAdvancedSearch" runat="server" Width="336px"></asp:TextBox>
                </td>
                            <td>
                    <asp:DropDownList ID="ddlSets1" runat="server"
                                    >
                        <asp:ListItem Text="Titulo"></asp:ListItem>
                        <asp:ListItem Text="Autor"></asp:ListItem>
                        <asp:ListItem Text="Contenido"></asp:ListItem>
                    </asp:DropDownList>
                </td>
                        </tr>
                   </table>
                </td>
            </tr>

                <caption>
                    <br />
                </caption>
            </table>
                <hr />
                    <asp:Button ID="btnAdvancedSearch" runat="server" Text="Buscar" 
                    align="right" onclick="btnAdvancedSearch_Click" />

            </asp:Panel>
            
            <asp:Panel ID="pnlAdvancedTitleResult" runat="server" Visible="False">

                <asp:SqlDataSource ID="SqlTitle" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ecommerceSample %>" 
                    SelectCommand="SELECT aspnet_Users.Username,  Articles.ArticleId, Articles.Date, aspnet_Users.UserId, Categories.CategoryId, Articles.Title, Articles.Image, Categories.Name, Articles.Text FROM Articles INNER JOIN Categories ON Articles.CategoryId = Categories.CategoryId INNER JOIN aspnet_Users ON Articles.UserId = aspnet_Users.UserId WHERE (Articles.Title LIKE '%' + @title + '%')">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="txbAdvancedSearch" Name="title" 
                            PropertyName="Text" />
                    </SelectParameters>
                </asp:SqlDataSource>
                <asp:SqlDataSource ID="SqlAutor" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ecommerceSample %>" 
                    SelectCommand="SELECT aspnet_Users.Username,  Articles.ArticleId, Articles.Date, aspnet_Users.UserId, Categories.CategoryId, Articles.Title, Articles.Image, Categories.Name, Articles.Text FROM Articles INNER JOIN Categories ON Articles.CategoryId = Categories.CategoryId INNER JOIN aspnet_Users ON Articles.UserId = aspnet_Users.UserId WHERE (aspnet_Users.Username LIKE '%' + @user + '%')">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="txbAdvancedSearch" Name="user" 
                            PropertyName="Text" />
                    </SelectParameters>
                </asp:SqlDataSource>

                <asp:SqlDataSource ID="SqlContent" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ecommerceSample %>" 
                    SelectCommand="SELECT aspnet_Users.Username,  Articles.ArticleId, Articles.Date, aspnet_Users.UserId, Categories.CategoryId, Articles.Title, Articles.Image, Categories.Name, Articles.Text FROM Articles INNER JOIN Categories ON Articles.CategoryId = Categories.CategoryId INNER JOIN aspnet_Users ON Articles.UserId = aspnet_Users.UserId WHERE (Articles.Text LIKE '%' + @content + '%')">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="txbAdvancedSearch" Name="content" 
                            PropertyName="Text" />
                    </SelectParameters>
                </asp:SqlDataSource>
                
                <asp:GridView ID="gvAdvancedSearchTitle" runat="server" AllowPaging="True" BorderColor="White"
                        AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" 
                    DataKeyNames="ArticleId" >
                    <Columns>
                         <asp:TemplateField>
                                <ItemTemplate>
                                <asp:Table ID="Table1" runat="server">
                                    <asp:TableRow ID="TableRow1" runat="server">
                                        <asp:TableCell ID="TableCell1" runat="server" >
                                             <a href="/User/Article.aspx?id=<%# Eval("ArticleId") %>"><img alt="<%# Eval("Title") %>" class="small-images"  src="<%# Eval("Image") %>" width="120px"/></a>
                                        </asp:TableCell>
                                        <asp:TableCell ID="TableCell2" runat="server" CssClass="text-content">                                            
                                            <hr />
                                            <div class="article-grey"><a href="/Category.aspx?id=<%# Eval("CategoryId") %>"><%# Eval("Name") %></a><small>: <%# Eval("Date") %></small></div>
                                                <b>
                                                <div class="article-title"><big><a href="/User/Article.aspx?id=<%# Eval("ArticleId") %>"><%# MarcarTitulo(Eval("Title").ToString())%></a></big></div>
                                                </b>
                                            <small>
                                                <div class="article-grey">Posteado por: <big><a href="/User/User.aspx?id=<%# Eval("UserId") %>"><%# MarcarAutor(Eval("UserName").ToString()) %></a></big></div>
                                                <%# MarcarContenido(Eval("Text").ToString())%>                                            
                                                <a href="/User/Article.aspx?id=<%# Eval("ArticleId") %>"><br />
                                                <div class="read-more"><i>leer más</i></a></div>
                                            </small>
                                        </asp:TableCell>
                                    </asp:TableRow>
                                </asp:Table>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                        <EmptyDataTemplate>
                            <asp:Label ID="lblAdvancedSearchError" runat="server" BorderColor="White" 
                                BorderStyle="None" Font-Bold="True" ForeColor="Red" 
                                Text="No existe resultados"></asp:Label>
                        </EmptyDataTemplate>
                </asp:GridView>
            </asp:Panel>

           
            </asp:View>


            <asp:View ID="vwSearchByIndex" runat="server">            
                    <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
                        ConnectionString="<%$ ConnectionStrings:ecommerceSample %>" 
                        ProviderName="<%$ ConnectionStrings:ecommerceSample.ProviderName %>"
                        SelectCommand="SELECT [Name] FROM [Categories]"></asp:SqlDataSource>
            
                <table >
                <tr>
                    <td>
                        <br />
                        <asp:Label ID="lblIndex" runat="server" Text="Category"></asp:Label>
                        <br />
                    </td>
                    <td>
                        <br />
                        <asp:DropDownList ID="ddlIndex" runat="server" DataSourceID="SqlDataSource2" 
                            DataTextField="Name" DataValueField="Name" AutoPostBack="true">
                        </asp:DropDownList>
                        <br />
                    </td>
                </tr>
                </table>

            <!-- INDEX DISPLAY-->
                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                    ConnectionString="<%$ ConnectionStrings:ecommerceSample %>" 
                    ProviderName="<%$ ConnectionStrings:ecommerceSample.ProviderName %>"
                    SelectCommand="SELECT Articles.ArticleId,Articles.Title FROM Articles INNER JOIN Categories ON Articles.CategoryId = Categories.CategoryId WHERE (Categories.Name = @name) ORDER BY Articles.Title">
                    <SelectParameters>
				<asp:ControlParameter ControlID="ddlIndex" Name="name" 
					PropertyName="SelectedValue" />
			</SelectParameters>
                </asp:SqlDataSource>
                <asp:GridView ID="GridView1" runat="server" AllowPaging="True" BorderColor="White"
                        AutoGenerateColumns="False" BorderStyle="None" BorderWidth="0px" DataKeyNames="ArticleId" 
                    DataSourceID="SqlDataSource1" PageSize="2">
                    <Columns>
                        <asp:TemplateField>
                            <ItemTemplate>
                                 <b>
                                 <a href="/User/Article.aspx?id=<%# Eval("ArticleId")%>">
                                 <%# Eval("Title") %>
                                 </a>                                 
                                 </b>
                                 <hr />   
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
            </asp:View>
        </asp:MultiView>    
    </div>
</asp:Content>
